package com.example.java_chatroom.Controller;

import com.example.java_chatroom.model.FriendApply;
import com.example.java_chatroom.model.Result;
import com.example.java_chatroom.model.UserInfo;
import com.example.java_chatroom.service.FriendApplyService;
import com.example.java_chatroom.service.FriendInfoService;
import com.example.java_chatroom.service.UserInfoService;
import com.example.java_chatroom.utils.GetUserInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RequestMapping("/friendApply")
@RestController
public class FriendApplyController {

    @Autowired
    private FriendApplyService friendApplyService;
    @Autowired
    private FriendInfoService friendInfoService;
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/turn_down")
    public void turn_down(String user) {
        friendApplyService.deleteMessage(user);
    }
    @RequestMapping("/accept")
    public Result accept(String user,HttpServletRequest request) {
        UserInfo userInfo = GetUserInfoUtils.getUserInfo(request);
        if (userInfo == null) {
            return Result.fail(null);
        }
        UserInfo userInfo1 = userInfoService.queryUserByName(user);
        if (userInfo1 == null) {
            return Result.fail(null);
        }
        friendApplyService.deleteMessage(user);
        friendInfoService.insertfriend(userInfo.getUserId(), userInfo1.getUserId());
        return Result.success(1);
    }
    @RequestMapping("/getFriendRequest")
    public Result getFriendRequest(HttpServletRequest request) {
        UserInfo userInfo = GetUserInfoUtils.getUserInfo(request);
        if (userInfo == null) {
            return Result.fail(null);
        }
       List<FriendApply> lists = friendApplyService.queryFriendName(userInfo.getUserName());
        if (lists == null) {
            return Result.fail(null);
        }
        return Result.success(lists);
    }
}
